﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;
using System.Data.SqlClient;
using System.Data;

namespace GoodMan.Portal.Biz
{
    public class AppraiseBiz
    {
        public static Appraise SaveAppraise(Appraise appraise)
        {
            appraise=appraise.PostAdd();
            //添加用户积分
            var integration = new Integration().Find(c => c.UserName == appraise.UserName);
            if (integration == null)
            {
                integration = new Integration()
                {
                    CostValue = appraise.AppraiseGrade,
                    UserName = appraise.UserName,
                    Balance = 0,
                    Activity = 0,
                    LockBalance=0
                };
                integration.PostAdd();
            }
            else
            {
                integration.CostValue = integration.CostValue + appraise.AppraiseGrade;
                integration.PostModify();
            }
            return appraise;
        }

        public static bool DelAppraise(int appraisId)
        {
            var appraise = new Appraise().Find(c=>c.Id== appraisId&&c.State==1);
            if (appraise != null)
            {
                string userName = appraise.UserName;
                appraise.State = 0;
                appraise.PostModify();
                //删除用户的积分
                var integration = new Integration().Find(c => c.UserName == userName.Trim());
                if (integration != null)
                {
                    integration.CostValue = integration.CostValue - appraise.AppraiseGrade;
                    integration.PostModify();
                }
            }
            return true;
        }

        public static ItemListBox<Appraise> GetAppraiseListByProductId(int productId,int page=0,int pageSize=10)
        {
            var count = new Appraise().Count(c => c.ProductId == productId && c.State == 1);

            using (Context ctx = new Context())
            {
                var data = (from c in ctx.Appraise
                            join d in ctx.User
                            on c.UserName equals d.UserName
                            where c.ProductId == productId&&c.State==1
                            orderby c.AppraiseDate descending
                            select new { c, d })
                          .Skip(page * pageSize)
                          .Take(pageSize)
                          .ToList()
                          .Select(e => new Appraise().Set(e.c).LoadUser(e.d))
                          .ToList();
               
                var list = new ItemListBox<Appraise>(data).BuildPage(
                    count, page, pageSize, new PageParameter() { Style = "home" });
                return list;
            }

            

        }

        /// <summary>
        /// 计算商家的评分
        /// </summary>
        /// <param name="bId"></param>
        /// <param name="pId"></param>
        /// <returns></returns>
        public static int CalcBusinessAppraise(int bId=0)
        {
            int result = 0;
            using (Context ctx = new Context())
            {
                string sql = @"select a.AppraiseGrade
                    from T_Appraise a 
                    left join T_Product p on a.ProductId=p.id
                    left join dbo.T_Business b on b.Id=p.BusinessId
                    where p.BusinessId=@bId";

                var datatable = ctx.ExcuteSQL(sql,
                     new SqlParameter("@bId", bId)
                    );

                if (datatable != null && datatable.Rows.Count > 0)
                {
                    foreach (DataRow row in datatable.Rows)
                    {
                        result += Convert.ToInt32(row["AppraiseGrade"]);
                    }
                    result = result / datatable.Rows.Count;
                }                
            }
            return result;
        }
    }
}
